package com.wzj.yygh.hosp.controller.admin;

import com.wzj.yygh.common.exception.YyghException;
import com.wzj.yygh.common.result.R;
import com.wzj.yygh.common.result.ResultCode;
import com.wzj.yygh.hosp.service.ScheduleService;
import com.wzj.yygh.model.hosp.Schedule;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "排班接口")
@RestController
@RequestMapping("admin/hosp/schedule")
//@CrossOrigin
public class AdminScheduleController {

    @Autowired
    private ScheduleService scheduleService;

    //根据医院编号和科室编号 查询排班规则数据
    @ApiOperation(value = "查询排班规则数据")
    @GetMapping("getScheduleRule/{page}/{limit}/{hoscode}/{depcode}")
    public R getScheduleRule(@PathVariable Long page,@PathVariable Long limit,@PathVariable String hoscode,@PathVariable String depcode) {
        Map<String,Object> map = scheduleService.getRuleSchedule(page,limit,hoscode,depcode);
        return R.ok().data("map",map);
    }

    //根据医院编号，科室编号和工作日期 查询排班详细信息
    @ApiOperation(value = "查询排班详细信息")
    @GetMapping("getScheduleDetail/{hoscode}/{depcode}/{workDate}")
    public R getScheduleDetail(@PathVariable String hoscode,@PathVariable String depcode,
                               @PathVariable String workDate){
        List<Schedule> list = scheduleService.getDetailSchedule(hoscode,depcode,workDate);
        return R.ok().data("list",list);
    }

    @ApiOperation(value = "获取排班信息")
    @GetMapping("getAllSchedule")
    public R getAllSchedule(){
        List<Schedule> list = scheduleService.getSchedule();
        return R.ok().data("list",list);
    }

    @ApiOperation("添加排班")
    @PostMapping("saveSchedule")
    public R saveSchedule(@RequestBody Schedule schedule) {
        //设置状态 1 使用 0 不能使用
        schedule.setStatus(1);
        //调用service
        boolean save = scheduleService.saveSchedule(schedule);
        if(save) {
            return R.ok().message("添加成功");
        } else {
            return R.error().message("添加失败");
        }
    }

    @ApiOperation("根据id获取排班")
    @GetMapping("getSchedule/{hosScheduleId}")
    public R getSchedule(@PathVariable String hosScheduleId) {
        //调用service
        Schedule schedule = scheduleService.getScheduleByHosScheduleId(hosScheduleId);
        return R.ok().data("item",schedule);
    }

    @ApiOperation("修改排班信息")
    @PutMapping("updateSchedule")
    public R updateDoctorSet(@RequestBody Schedule schedule) {
        boolean flag = scheduleService.updateByHosScheduleId(schedule);
        if(flag) {
            return R.ok().message("修改成功");
        } else {
            return R.error().message("修改失败");
        }
    }

    @ApiOperation(value = "删除排班信息")
    @DeleteMapping("remove/{hosScheduleId}")
    public R removeDoctor(
            @ApiParam(value = "排班信息id",required = true)
            @PathVariable String hosScheduleId) {
        try {
            boolean flag = scheduleService.removeByHosScheduleId(hosScheduleId);
            if(flag) {
                return R.ok().message("删除成功");
            } else {
                return R.error().message("删除失败").code(ResultCode.ERROR_DELETE);
            }
        } catch (Exception e) {
            throw new YyghException(ResultCode.ERROR,"删除排班信息错误",e);
        }
    }

}
